from django.db import models
from django.contrib.auth.models import AbstractUser


class UserInfo(AbstractUser):
    """用户信息表"""
    telephone = models.CharField(verbose_name="用户手机", max_length=11, null=True)
    avatar = models.FileField(verbose_name="用户头像文件路径", upload_to='avatar/', default='avatar/default.png')

    def __str__(self):
        return self.username


class Customer(models.Model):
    """客户信息"""
    cid = models.CharField(verbose_name="客户ID", max_length=255, primary_key=True, unique=True)
    name = models.CharField(verbose_name="客户名称", max_length=255)
    category = models.CharField(verbose_name="客户细分", max_length=255)

    def __str__(self):
        return f"({self.cid}) {self.name}"

    class Meta:
        db_table = "tb_customer"
        verbose_name = "客户信息"
        verbose_name_plural = verbose_name


class Order(models.Model):
    """购买信息"""
    id = models.BigIntegerField(verbose_name="信息ID", primary_key=True, unique=True)
    oid = models.CharField(verbose_name="订单ID", max_length=255)
    created = models.DateField(verbose_name="订单日期")
    mail = models.DateField(verbose_name="发货日期")
    mail_type = models.CharField(verbose_name="邮寄方式", max_length=255)
    customer = models.ForeignKey(Customer, on_delete=models.CASCADE, verbose_name="对应客户")
    city = models.CharField(verbose_name="城市", max_length=255)
    province = models.CharField(verbose_name="省/自治区", max_length=255)
    nation = models.CharField(verbose_name="国家", max_length=255)
    area = models.CharField(verbose_name="地区", max_length=255)
    pid = models.CharField(verbose_name="产品ID", max_length=255)
    category = models.CharField(verbose_name="类别", max_length=255)
    subcategory = models.CharField(verbose_name="子类别", max_length=255)
    name = models.CharField(verbose_name="产品名称", max_length=255)
    revenue = models.DecimalField(verbose_name="销售额", max_digits=10, decimal_places=3)
    quantity = models.IntegerField(verbose_name="数量", default=1)
    discount = models.DecimalField(verbose_name="折扣", max_digits=5, decimal_places=2)
    profit = models.DecimalField(verbose_name="利润", max_digits=10, decimal_places=3)

    def __str__(self):
        return f"({self.id}) {self.oid}订单的信息"

    class Meta:
        db_table = "tb_order"
        verbose_name = "购买信息"
        verbose_name_plural = verbose_name


class Salesman(models.Model):
    """销售人员"""
    area = models.CharField(verbose_name="地区", max_length=255)
    manager = models.CharField(verbose_name="地区经理", max_length=255)

    def __str__(self):
        return f"{self.area}地区经理 {self.manager}"

    class Meta:
        db_table = "tb_salesman"
        verbose_name = "销售人员"
        verbose_name_plural = verbose_name
